﻿using System.Web.Mvc;
using System.Web.Routing;

namespace kTudu.Web.HTML
{
    public static class LinkExtensions
    {
        public static MvcHtmlString ActionImage(this HtmlHelper html, string action, object routeValues, string imagePath, string alt)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);

            // build the <img> tag
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", url.Content(imagePath));
            imgBuilder.MergeAttribute("alt", alt);
            string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");
            anchorBuilder.MergeAttribute("href", url.Action(action, routeValues));
            anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
            string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(anchorHtml);
        }

        public static MvcHtmlString ImageActionLink(this HtmlHelper helper, string imagePath, string actionName, string controllerName, object routeValues, object imageAttributes, object linkAttributes)
        {
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);

            var aBuilder = new TagBuilder("a");
            aBuilder.MergeAttributes(new RouteValueDictionary(linkAttributes));
            aBuilder.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));

            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttributes(new RouteValueDictionary(imageAttributes));
            imgBuilder.MergeAttribute("src", imagePath);

            aBuilder.InnerHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

            return new MvcHtmlString(aBuilder.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ButtonImageActionLink(this HtmlHelper html, string value, string action, string controllerName = null, string imageClass = "", string buttonClass = "", string id = null, object routeValues = null)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);

            var span1 = new TagBuilder("span");
            var span2 = new TagBuilder("span");
            var span3 = new TagBuilder("span");
            span3.MergeAttribute("class", imageClass);
            span3.InnerHtml = value;

            span2.InnerHtml = span3.ToString(TagRenderMode.Normal);
            span1.InnerHtml = span2.ToString(TagRenderMode.Normal);

            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");
            anchorBuilder.MergeAttribute("class", "custombutton " + buttonClass);

            if (!string.IsNullOrEmpty(id))
            {
                anchorBuilder.MergeAttribute("Id", id);
            }

            anchorBuilder.MergeAttribute("href",
                                         string.IsNullOrEmpty(controllerName)
                                             ? url.Action(action, routeValues)
                                             : url.Action(action, controllerName, routeValues));

            anchorBuilder.InnerHtml = span1.ToString(TagRenderMode.Normal);
            string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(anchorHtml);
        }

        /// <summary>
        /// Boton con imagen.
        /// </summary>
        ///<remarks>En caso de ser submit requiere que se defina un metodo llamado "submit" que realize el submit del form.
        /// Existe un metodo submit general en asp.net.js, si se requiere logica particular para el submit se debe escribir uno en el js particular de la vista.
        /// </remarks>
        /// <returns></returns>
        public static MvcHtmlString ButtonImage(this HtmlHelper html, string id, string imageClass, string buttonClass, string value, bool isSubmit)
        {
            var span1 = new TagBuilder("span");
            var span2 = new TagBuilder("span");
            var span3 = new TagBuilder("span");
            span3.MergeAttribute("class", imageClass);
            span3.InnerHtml = value;

            span2.InnerHtml = span3.ToString(TagRenderMode.Normal);
            span1.InnerHtml = span2.ToString(TagRenderMode.Normal);

            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");
            anchorBuilder.MergeAttribute("class", "custombutton " + buttonClass);
            anchorBuilder.MergeAttribute("Id", id);

            if (isSubmit)
                anchorBuilder.MergeAttribute("onclick", "submit();");

            anchorBuilder.InnerHtml = span1.ToString(TagRenderMode.Normal);


            var anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(anchorHtml);
        }
    }
}